package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserApproveMapper;
import org.linlinjava.litemall.db.dao.LitemallUserHerbApproveItemMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallUserAuditService {
    @Resource
    private LitemallUserApproveMapper userApproveMapper;

    @Resource
    private LitemallUserHerbApproveItemMapper UserHerbApproveItemMapper;

    public List<LitemallUserApprove> querySelective(String approveStatus,String userType,String companyName, String sort, String order, Integer page, Integer size) {
        LitemallUserApproveExample example = new LitemallUserApproveExample();
        LitemallUserApproveExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userType)){
            criteria.andUserTypeEqualTo(Byte.valueOf(userType));
        }

        if (!StringUtils.isEmpty(companyName)){
            criteria.andCompanyNameLike("%"+companyName+"%");
        }

        if (!StringUtils.isEmpty(approveStatus)) {
            criteria.andApproveStatusEqualTo(Byte.valueOf(approveStatus));
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userApproveMapper.selectByExample(example);
    }

    public List<LitemallUserHerbApproveItem> queryHerbList(String userApproveId) {
        LitemallUserHerbApproveItemExample example = new LitemallUserHerbApproveItemExample();
        LitemallUserHerbApproveItemExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(userApproveId)) {
            criteria.andUserApproveIdEqualTo(Integer.valueOf(userApproveId));
        }
        criteria.andDeletedEqualTo(false);
        return UserHerbApproveItemMapper.selectByExample(example);
    }

    public int updateById(LitemallUserApprove userApprove) {
        userApprove.setUpdateTime(LocalDateTime.now());
        return userApproveMapper.updateByPrimaryKeySelective(userApprove);
    }

    public LitemallUserHerbApproveItem findById(String userApproveId) {
        return UserHerbApproveItemMapper.selectByPrimaryKey(Integer.valueOf(userApproveId));
    }



    public void delete(LitemallUserHerbApproveItem userApprove) {
        userApprove.setUpdateTime(LocalDateTime.now());
        userApprove.setDeleted(true);
        UserHerbApproveItemMapper.updateByPrimaryKeySelective(userApprove);
    }

    public void batchInsert(List<LitemallUserHerbApproveItem> userApprove) {
        UserHerbApproveItemMapper.batchInsert(userApprove);
    }

    /**
     * 获取待审核总数
     *
     * @return
     */
    public int count() {
    	LitemallUserApproveExample example = new LitemallUserApproveExample();
        LitemallUserApproveExample.Criteria criteria = example.createCriteria();
        
        criteria.andApproveStatusEqualTo((byte)0);
        criteria.andDeletedEqualTo(false);
        return (int) userApproveMapper.countByExample(example);
    }

}
